import 'package:flutter/material.dart';

import 'package:flutter/cupertino.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hockey/jzx_apps.dart';

class PrimaryButton extends StatelessWidget {
  const PrimaryButton({
    super.key,
    required this.child,
    required this.onPressed,
    this.padding = const EdgeInsets.all(5),
    this.margin,
    this.backgroundColor,
    this.borderColor,
    this.height = 54,
    this.radius = 100,
    this.gradient,
  });

  final Widget child;
  final EdgeInsets padding;
  final EdgeInsets? margin;
  final Color? backgroundColor;
  final Color? borderColor;
  final double height;
  final double radius;
  final Gradient? gradient;
  final VoidCallback? onPressed;

  @override
  Widget build(BuildContext context) {
    return CupertinoButton(
      pressedOpacity: 0.8,
      padding: EdgeInsets.zero,
      onPressed: onPressed,
      child: Container(
        alignment: Alignment.center,
        padding: padding,
        margin: margin,
        height: height.w,
        width: margin == null ? null : double.infinity,
        decoration: BoxDecoration(
            color: (backgroundColor ?? context.appColors.primary)
                .withOpacity(onPressed == null ? 0.8 : 1),
            borderRadius: BorderRadius.circular(radius),
            border: Border.all(
              color: borderColor != null ? borderColor! : Colors.transparent,
              width: 1,
            ),
            gradient: gradient),
        child: child,
      ),
    );
  }
}

/// 通用提交按钮
class SubmitButton extends StatelessWidget {
  const SubmitButton(
    this.title, {
    super.key,
    required this.onPressed,
    this.backgroundColor,
    this.margin = const EdgeInsets.symmetric(horizontal: 24),
    this.radius = 100,
    this.style,
  });

  final String title;
  final EdgeInsets? margin;
  final Color? backgroundColor;
  final VoidCallback? onPressed;
  final double radius;
  final TextStyle? style;

  @override
  Widget build(BuildContext context) {
    return PrimaryButton(
      backgroundColor: backgroundColor,
      onPressed: onPressed,
      margin: margin,
      radius: radius,
      child: Text(title, style: style ?? myAppColors.black.font_12),
    );
  }
}

class FeedbackButton extends StatelessWidget {
  const FeedbackButton({
    super.key,
    required this.child,
    required this.onPressed,
  });

  final Widget child;
  final VoidCallback? onPressed;

  @override
  Widget build(BuildContext context) {
    return CupertinoButton(
      pressedOpacity: 0.8,
      padding: EdgeInsets.zero,
      borderRadius: BorderRadius.circular(0),
      minSize: 10,
      onPressed: onPressed,
      child: child,
    );
  }
}

class GradientButton extends StatelessWidget {
  const GradientButton(
    this.title, {
    super.key,
    this.onPressed,
    this.style,
    this.height = 54,
    this.width,
    this.gradient,
  });

  final String title;
  final TextStyle? style;

  final double? height;
  final double? width;

  final Gradient? gradient;
  final VoidCallback? onPressed;

  @override
  Widget build(BuildContext context) {
    return FeedbackButton(
      onPressed: onPressed,
      child: Container(
        width: width,
        height: height,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(100),
            gradient: gradient ??
                const LinearGradient(colors: [
                  Color(0xFF3FE1D8),
                  Color(0xFF63FF6A),
                ], begin: Alignment.centerLeft, end: Alignment.centerRight),
            boxShadow: const [
              BoxShadow(
                  color: Color(0xFF3FE1D8),
                  offset: Offset(0, 0),
                  blurRadius: 12,
                  spreadRadius: 0),
            ]),
        child: Text(
          title,
          style: style ??
              GoogleFonts.racingSansOne(
                fontSize: 24.sp,
                color: Colors.white,
              ),
        ),
      ),
    );
  }
}

/// 纯文字按钮
class TextColorButton extends CustomButton {
  const TextColorButton(
    this.title, {
    this.style,
    super.key,
    super.onPressed,
    super.width,
    super.height,
    super.padding,
    super.margin,
    super.backgroundColor,
    super.borderColor,
    super.raduis,
  });

  final String title;

  final TextStyle? style;

  @override
  Widget build(BuildContext context) {
    return CustomButton(
      onPressed: onPressed,
      backgroundColor: backgroundColor,
      margin: margin,
      padding: padding,
      width: width,
      height: height,
      raduis: raduis,
      borderColor: borderColor,
      child: Text(title, style: style ?? myAppColors.white.font_14),
    );
  }
}

/// 自定义按钮
class CustomButton extends StatelessWidget {
  const CustomButton({
    super.key,
    this.child,
    this.width,
    this.height,
    this.padding,
    this.margin,
    this.backgroundColor,
    this.borderColor,
    this.raduis,
    this.onPressed,
  });

  final double? height;
  final double? width;

  final double? raduis;

  final EdgeInsets? padding;
  final EdgeInsets? margin;

  final Color? backgroundColor;
  final Color? borderColor;

  final Widget? child;

  final VoidCallback? onPressed;

  @override
  Widget build(BuildContext context) {
    final bgColor = backgroundColor ?? myAppColors.primary;

    return FeedbackButton(
      onPressed: onPressed,
      child: Container(
        width: width,
        height: height,
        padding:
            padding ?? const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
        margin: margin,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          color: bgColor.withOpacity(onPressed == null ? 0.7 : 1),
          borderRadius: BorderRadius.circular(raduis ?? 4),
          // border: Border.all(
          //     width: 1,
          //     color: (borderColor ?? bgColor)
          //         .withOpacity(onPressed == null ? 0.7 : 1)),
        ),
        child: child,
      ),
    );
  }
}
